﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 委外配料
    /// </summary>
    public class OutSourceBatchService : BaseService<BaseEntity>, IOutSourceBatchService
    {
        private readonly IOutSourceBatchRepository repository;
        public OutSourceBatchService(IOutSourceBatchRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        public SetObjectDetail GetOutSourceBatchOrderList(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

            List<WmsOutstock> wmsMoves = repository.GetOutSourceBatchOrderList(orderNo);

            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail GetOutSourceBatchOrderListByOrderNo(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            List<WmsOutstockDetail> wmsMoves = repository.GetOutSourceBatchOrderListByOrderNo(orderNo);

            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }

        public SetObjectDetail GetStockByCartonNo(string cartonNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            List<SysStock> sysStocks = repository.GetStockByCartonNo(cartonNo);

            if (sysStocks.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = sysStocks.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "箱条码不存在，请检查！";
            }
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string orderNo, string groupNo, string isSplit, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            //配料列表
            List<WmsOutstockDetail> details = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);
            //扫描到的箱条码
            SysStock Stock = repository.GetCartonInfo(cartonNo);

            if (Stock == null)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return rfInfo;
            }
            if (Stock.Enabled.Equals("N"))
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                return rfInfo;
            }
            if (Stock.Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                return rfInfo;
            }
            if (Stock.Qty < 1)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return rfInfo;
            }

            List<WmsOutstockDetail> ifScan = details.Where(a => a.PartId == Stock.PartId).ToList();
            if (ifScan.Count == 0)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:不在委外发运单中，请检查！";
                return rfInfo;
            }

            if (ifScan[0].FactoryId != Stock.FactoryId)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:与委外发运单不在同工厂，请检查！";
                return rfInfo;
            }

            if (ifScan[0].PickQty >= ifScan[0].Qty)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + ifScan[0].PartNo + "]:已完成配料！";
                return rfInfo;
            }

            ////获取零件号 最早批次列表
            //List<SysStock> PartByFIFO = repository.GetPartByFIFO(Stock.PartId.ToString());
            //List<SysStock> isSure = PartByFIFO.Where(a => a.CartonNo == Stock.CartonNo).ToList();
            //if (isSure.Count == 0)
            //{
            //    rfInfo.type = "FAIL";
            //    rfInfo.message = "[" + cartonNo + "]:箱条码不是最早批次，请扫描[" + PartByFIFO[0].CartonNo + "]";
            //    return rfInfo;
            //}

            if ((ifScan[0].PickQty + Stock.Qty) > ifScan[0].Qty && isSplit == "False")
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码为尾箱超出配料数，请开启拆分进行扫描！";
                return rfInfo;
            }

            if (isSplit == "True")
            {
                //尾箱拆分
                decimal needNum = ifScan[0].Qty - ifScan[0].PickQty; //需要拆的数量
                if (needNum >= Stock.Qty)
                {
                    //不需要拆 
                }
                else
                {
                    //原条码
                    decimal oldQty = Stock.Qty - needNum;
                    CheckMaterialSplit(Stock.CartonNo, Convert.ToInt32(oldQty), loginId, 1);
                    //更新拆分后数据
                    Stock = repository.GetCartonInfo(cartonNo);
                    return repository.UpdteStatus(orderNo, ifScan, Stock, groupNo, loginId);
                }
            }

            return repository.UpdteStatus(orderNo, ifScan, Stock, groupNo, loginId);
        }

        public SetObjectDetail CheckStockByCartonAlocateGroup(string orderNo, string groupNo, string isSplit, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            //配料列表
            List<WmsOutstockDetail> details = repository.CheckMoveDetail(orderNo, (int)WmsEnumUtil.StockStatus.INSTOCKED);

            List<SysStock> sysStocks = repository.GetCartonInfoByGroupNo(groupNo);
            for (int i = 0; i < sysStocks.Count; i++)
            {
                SysStock Stock = sysStocks[i];
                string cartonNo = Stock.CartonNo;
                if (Stock == null)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                    return rfInfo;
                }
                if (Stock.Enabled.Equals("N"))
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                    return rfInfo;
                }
                if (Stock.Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                    return rfInfo;
                }
                if (Stock.Qty < 1)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                    return rfInfo;
                }

                List<WmsOutstockDetail> ifScan = details.Where(a => a.PartId == Stock.PartId).ToList();
                if (ifScan.Count == 0)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:不在委外发运单中，请检查！";
                    return rfInfo;
                }

                if (ifScan[0].FactoryId != Stock.FactoryId)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:与委外发运单不在同工厂，请检查！";
                    return rfInfo;
                }

                if (ifScan[0].PickQty >= ifScan[0].Qty)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + ifScan[0].PartNo + "]:已完成配料！";
                    return rfInfo;
                }

                ////获取零件号 最早批次列表
                //List<SysStock> PartByFIFO = repository.GetPartByFIFO(Stock.PartId.ToString());
                //List<SysStock> isSure = PartByFIFO.Where(a => a.CartonNo == Stock.CartonNo).ToList();
                //if (isSure.Count == 0)
                //{
                //    rfInfo.type = "FAIL";
                //    rfInfo.message = "[" + cartonNo + "]:箱条码不是最早批次，请扫描[" + PartByFIFO[0].CartonNo + "]";
                //    return rfInfo;
                //}

                if ((ifScan[0].PickQty + Stock.Qty) > ifScan[0].Qty && isSplit == "False")
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:箱条码为尾箱超出配料数，请开启拆分进行扫描！";
                    return rfInfo;
                }

                if (isSplit == "True")
                {
                    //尾箱拆分
                    decimal needNum = ifScan[0].Qty - ifScan[0].PickQty; //需要拆的数量
                    if (needNum >= Stock.Qty)
                    {
                        //不需要拆 
                    }
                    else
                    {
                        //原条码
                        decimal oldQty = Stock.Qty - needNum;
                        CheckMaterialSplit(Stock.CartonNo, Convert.ToInt32(oldQty), loginId, 1);
                        //更新拆分后数据
                        Stock = repository.GetCartonInfo(cartonNo);
                        return repository.UpdteStatus(orderNo, ifScan, Stock, groupNo, loginId);
                    }
                }

                repository.UpdteStatus(orderNo, ifScan, Stock, groupNo, loginId);
            }

            List<WmsOutstockDetail> wmsMoves1 = repository.getWmsOutstockDetailInfo(orderNo);
            for (int i = 0; i < wmsMoves1.Count; i++)
            {
                if (wmsMoves1[i].Qty != wmsMoves1[i].PickQty)
                {
                    rfInfo.type = "PASS";
                    rfInfo.message = "操作成功!请继续扫描箱条码";
                    rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                    return rfInfo;
                }
            }
            rfInfo.type = "PASS";
            rfInfo.message = "单据操作完成!请切换单据或移至委外发运";
            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
            return rfInfo;
        }

        public SetObjectDetail CheckMaterialSplit(string cartonNo, int splitNum, string loginId, int splitCount)
        {

            SetObjectDetail det = new SetObjectDetail();
            //需要拆分的箱条码信息
            List<SysStock> MaterialInfo = repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码已被冻结，请检查!";
                return det;
            }
            //if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            //{
            //    det.type = "NO";
            //    det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
            //    return det;
            //}
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            List<SysStock> NewStock = repository.CheckMaterialSplit(MaterialInfo, splitNum, loginId, splitCount);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }

    }
}
